package cn.wolfcode.web.controller;

import cn.wolfcode.anno.RequirePermission;
import cn.wolfcode.domain.Permission;
import cn.wolfcode.qo.JsonResult;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.service.IPermissionService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.*;
import java.util.stream.Collectors;


@Controller
@RequestMapping("/permission")
public class PermissionController {

    @Autowired
    private IPermissionService PermissionService;

    @Autowired
    private ApplicationContext ctx;

    // 保存之前看数据库是否已存在 , 不存在才保存 , 把所有数据差出来

    @RequestMapping("/list")
    @RequirePermission(name = "权限查询", expression = "permission:list")
    public String List(QueryObject qo, Model model) {
        model.addAttribute("qo", qo);
        PageInfo<Permission> pageInfo = PermissionService.query(qo);
        model.addAttribute("pageInfo", pageInfo);
        return "permission/list";
    }

    @RequestMapping("/load")
    @ResponseBody
    public JsonResult load() {
        try {
            List<Permission> permissions = PermissionService.selectAll();
            Set<Permission> savedPermissions = ctx.getBeansWithAnnotation(Controller.class).values().stream()
                    .flatMap(Controller -> Arrays.stream(Controller.getClass().getDeclaredMethods()))
                    .filter(method -> method.getAnnotation(RequirePermission.class) != null)
                    .map(method -> new Permission(method.getAnnotation(RequirePermission.class).name(), method.getAnnotation(RequirePermission.class).expression()))
                    .filter(permission -> !permissions.contains(permission))
                    .collect(Collectors.toSet());

            if (!savedPermissions.isEmpty()){
                PermissionService.batchInsert(savedPermissions);
            }
            return new JsonResult(true, "加载成功");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new JsonResult(false, "加载失败");
    }

    /*@RequestMapping("/load")
    @ResponseBody
    public JsonResult load(){

        List<Permission> permissions = PermissionService.selectAll();
        // 用来存储即将要保存的Permission对象
        // LinkedHashSet 有序且去重
        Set<Permission> savedPermissions = new LinkedHashSet<>();
        try{
            // 通过反射获取所有控制器的处理方法
            // 获取bean,且这个 bean 的类上贴 @Controller 注解
            Map<String, Object> beans = ctx.getBeansWithAnnotation(Controller.class);
            // beans 的value值是控制器类对象
            Collection<Object> controllers = beans.values();
            // 遍历所有的控制器对象
            for (O                // 若方法上贴了自定义注解,获取注解值封装成对象
                    if (annotation != null) {
                        String name = annotation.name();
                        String expression = annotation.expression();
                        Permission permission = new Permission();
                        permission.setName(name);
                        permission.setExpression(expression);
                        // 数据库不存在往数据库保存
                        if (!permissions.contains(permission)){
                            // PermissionService.insert(permission);
                            savedPermissions.add(permission);
                        }
                    }
                }
            }
            if (!savedPermissions.isEmpty()){
                PermissionService.batchInsert(savedPermissions);
            }

        }catch (Exception e){bject controller : controllers) {
                // 获取每个控制器的字节码对象 , 获取方法
                for (Method method : controller.getClass().getDeclaredMethods()) {
                    // 获取方法上的自定义注解
                    RequirePermission annotation = method.getAnnotation(RequirePermission.class);

            e.printStackTrace();
            return new JsonResult(false,"加载失败");
        }
        return new JsonResult(true,"加载成功");
    }*/

    /*@RequestMapping("/noPermission")
    public String noPermission(){
        return "common/nopermission";
    }*/
}
